package com.miguo.app;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import com.miguo.activity.AboutActivity;
import com.miguo.activity.AdvertActivity;
import com.miguo.activity.BoundPhoneActivity;
import com.miguo.activity.BrandItemActivity;
import com.miguo.activity.ChouJiangRecordActivity;
import com.miguo.activity.FavorActivity;
import com.miguo.activity.GuideActivity;
import com.miguo.activity.ItemActivity;
import com.miguo.activity.LoginActivity;
import com.miguo.activity.MainActivity;
import com.miguo.activity.PromotionActivity;
import com.miguo.activity.ResetPasswordActivity;
import com.miguo.activity.ScanCaptureActivity;
import com.miguo.activity.ScoreShopActivity;
import com.miguo.activity.SearchActivity;
import com.miguo.activity.SettingActivity;
import com.miguo.activity.SignActivity;
import com.miguo.activity.SignRecordActivity;
import com.miguo.activity.TbInfoActivity;
import com.miguo.activity.UserAccountActivity;
import com.miguo.activity.WebViewActivity;
import com.miguo.activity.YuGaoActivity;
import com.miguo.activity.auth.AuthWebActivity;
import com.miguo.bean.AdvertBean;
import com.miguo.bean.GoodsBean;
import com.miguo.framework.utils.Toast;
import com.miguo.http.Url;
import com.miguo.plugins.Statistics;
import com.miguo.plugins.alibaba.BaiChuan;
import com.miguo.ui.R;
import com.mrper.model.entry.brand.BrandThemeInfoEntry;
import com.mrper.ui.brand.BrandDetailActivity;
import com.mrper.ui.goods.GoodsDetailInfoActivity;

/**
 * 应用程序UI工具包： 封装UI相关的一些操作
 */
public class AppHelper {

    /**
     * 首屏广告
     *
     * @param advert
     */
    public static void StartAdvert(Activity activity, AdvertBean advert) {
        Intent in = new Intent(activity, AdvertActivity.class);
        in.putExtra("Advert", advert);
        activity.startActivity(in);
        activity.finish();
    }

    public static void StartGuide(Activity activity) {
        Intent in = new Intent(activity, GuideActivity.class);
        activity.startActivity(in);
        activity.finish();
    }

    /**
     * 首页
     */
    public static void StartMain(Activity activity) {
        Intent in = new Intent(activity, MainActivity.class);
        activity.startActivity(in);
        activity.finish();
    }

    public static void StartMain(Activity activity, String Display) {
        Intent in = new Intent(activity, MainActivity.class);
        in.putExtra("Display", Display);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
        activity.startActivity(in);
    }

    /**
     * 专题活动
     *
     * @param id
     * @param img
     * @param title
     */
    public static void StartPromotion(Activity activity, String title,
                                      String img, String id) {
        StartPromotionDispose(activity, title, img, id, true);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
    }

    public static void StartPromotion(Activity activity, String title,
                                      String img, String id, boolean back) {
        StartPromotionDispose(activity, title, img, id, back);
    }

    private static void StartPromotionDispose(Activity activity, String title,
                                              String img, String id, boolean back) {
        Intent in = new Intent(activity, PromotionActivity.class);
        in.putExtra("promotion_title", title);
        in.putExtra("promotion_img", img);
        in.putExtra("promotion_id", id);
        in.putExtra("back_btn", back);
        activity.startActivity(in);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_10);
    }

    /**
     * 品牌商品
     *
     * @param id
     * @param img
     * @param title
     */
    public static void StartBrandItem(Activity activity, String title,
                                      String img, String id) {
        StartBrandItemDispose(activity, title, img, id, true);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
    }

    public static void StartBrandItem(Activity activity, String title,
                                      String img, String id, boolean back) {
        StartBrandItemDispose(activity, title, img, id, back);
    }

    private static void StartBrandItemDispose(Activity activity, String title,
                                              String img, String id, boolean back) {
        Intent in = new Intent(activity, BrandItemActivity.class);
        in.putExtra("brand_title", title);
        in.putExtra("brand_img", img);
        in.putExtra("brand_id", id);
        in.putExtra("back_btn", back);
        activity.startActivity(in);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_33);
    }

    /** 启动品牌商品列表内页 2016-08-24  **/
    public static void startBrandDetailListPage(Activity activity, BrandThemeInfoEntry info){
        Intent intent = new Intent(activity, BrandDetailActivity.class);
        intent.putExtra("brandThemeInfo", info);
        activity.startActivity(intent);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_49);
    }

    /**
     * 我的收藏
     */
    public static void StartFavor(Activity activity) {
        Intent in = new Intent(activity, FavorActivity.class);
        activity.startActivity(in);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_7);
    }

    public static void StartFavor(Activity activity, int position) {
        Intent in = new Intent(activity, FavorActivity.class);
        in.putExtra("position", position);
        activity.startActivity(in);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_7);
    }

    /**
     * 扫一扫To收藏
     */
    public static void StartScanFavor(Activity activity) {
        Intent in = new Intent(activity, FavorActivity.class);
        activity.startActivity(in);
        activity.finish();
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_31);
    }

    /**
     * WEB淘宝
     */
    public static void StartTBWebView(Activity activity, GoodsBean goods,
                                      boolean isTBGoods) {
        Intent in = new Intent(activity, TbInfoActivity.class);
        in.putExtra("goods", goods);
        in.putExtra("isTBGoods", isTBGoods);
        activity.startActivity(in);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_12);
    }

    /**
     * WEB
     */
    public static void StartWebView(Activity activity, String url) {
        StartWebViewDispose(activity, url, true);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
    }

    public static void StartWebView(Activity activity, String url, boolean back) {
        StartWebViewDispose(activity, url, back);
    }

    private static void StartWebViewDispose(Activity activity, String url, boolean back) {
        Intent in = new Intent(activity, WebViewActivity.class);
        in.putExtra("url", url);
        in.putExtra("back_btn", back);
        activity.startActivity(in);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_13);
    }


    /*积分商城*/
    public static void StartScoreShop(Activity activity) {
        Intent in = new Intent(activity, ScoreShopActivity.class);
        activity.startActivity(in);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
    }


    /**
     * 商品页
     */

    public static void StartItem(Activity activity, String title, String id,
                                 String url) {
        StartItemDispose(activity, title, id, url, true);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
    }

    public static void StartItem(Activity activity, String title, String id,
                                 String url, boolean back) {
        StartItemDispose(activity, title, id, url, back);
    }

    private static void StartItemDispose(Activity activity, String title, String id,
                                         String url, boolean back) {
        Intent in = new Intent(activity, ItemActivity.class);
        in.putExtra("title", title);
        in.putExtra("id", id);
        in.putExtra("url", url);
        in.putExtra("back_btn", back);
        activity.startActivity(in);
        Statistics.onMenuClick(activity, AppConfig.STATISTICS.EVENT_NAME_6, id);
    }

    /**
     * 去往商品详情页,新增时间：2016-03-13
     */
    public static void startGoodsDetailPage(Activity activity,GoodsBean goodsinfo){
        //跳转类型 1:自身web 2:自身详细页
        if(AppContext.GlobalJumpType == 2){
            Intent intent = new Intent(activity, GoodsDetailInfoActivity.class);
            intent.putExtra("goodsinfo",goodsinfo);
            activity.startActivity(intent);
            activity.overridePendingTransition(R.anim.push_left_in,0);
        }else { //代码分支添加时间：2016-04-26
            if (AppContext.isAilError) {
                AppHelper.StartTBWebView(activity, goodsinfo, true);
            } else {
                if (goodsinfo.getJump_type() == 2 || TextUtils.isEmpty(goodsinfo.getClick_url())) {
                    //阿里百川
                    BaiChuan.showTaokeItemDetailV2(activity, goodsinfo.getTitle(), goodsinfo.getItem_id(), goodsinfo.getItem_type());
                } else {
                    if (goodsinfo.getJump_type() == 3) {
                        BaiChuan.showPage(activity, goodsinfo.getTitle(), goodsinfo.getClick_url());
                    } else {
                        AppHelper.StartTBWebView(activity, goodsinfo, true);
                    }
                }
            }
        }
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_47);
    }


    /**  根据ID跳转至商品的详情信息页面  **/
    public static void startGoodsDetailPageByBrandGoodsId(Activity activity,String title, Long itemId, Integer jumpType){
        BaiChuan.showTaokeItemDetailV2(activity, title, itemId, jumpType); //阿里百川
    }

    /**
     * 搜索
     */
    public static void StartSearch(Activity activity, String keyWrod) {
        Intent in = new Intent(activity, SearchActivity.class);
        if (null != keyWrod) {
            in.putExtra("keyWord", keyWrod);
        }
        activity.startActivity(in);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_11);
    }

    /**
     * 关于我们
     */
    public static void StartAbout(Activity activity) {
        Intent in = new Intent(activity, AboutActivity.class);
        activity.startActivity(in);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_8);
    }

    /**
     * 设置
     */
    public static void StartSetting(Activity activity) {
        Intent in = new Intent(activity, SettingActivity.class);
        activity.startActivity(in);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_34);
    }

    /**
     * 米果账号登录(用的startActivityForResult跳转界面，需要获取返回结果,RESULT_CODE =
     * Activity.RESULT_FIRST_USER=1)
     */
    public static void StartLogin(Activity activity) {
        Intent in = new Intent(activity, LoginActivity.class);
        activity.startActivityForResult(in, Activity.RESULT_FIRST_USER);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_35);
    }

    /**
     * 账户退出
     *
     * @param activity
     */
    public static void StartUserAccount(Activity activity) {
        Intent in = new Intent(activity, UserAccountActivity.class);
        activity.startActivityForResult(in, Activity.RESULT_FIRST_USER);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_37);
    }

    /*
     * 重设密码
     */
    public static void StartResetPassword(Activity activity) {
        Intent in = new Intent(activity, ResetPasswordActivity.class);
        activity.startActivity(in);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_38);
    }

    /**
     * 绑定手机号
     *
     * @param activity
     */
    public static void StartBoundPhone(Activity activity) {
        Intent in = new Intent(activity, BoundPhoneActivity.class);
        activity.startActivity(in);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_42);
    }

    /**
     * 扫一扫
     */
    public static void StartScanCapture(Activity activity) {
        Intent in = new Intent(activity, ScanCaptureActivity.class);
        activity.startActivity(in);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_30);
    }

    /**
     * 积分记录
     */
    public static void StartSignRecord(Activity activity) {
        Intent in = new Intent(activity, SignRecordActivity.class);
        activity.startActivity(in);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_43);
    }

    /**
     * 我的抽奖
     */
    public static void StartChouJiangRecord(Activity activity) {
        Intent in = new Intent(activity, ChouJiangRecordActivity.class);
        activity.startActivity(in);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_40);
    }

    /**
     * 签到
     */
    public static void StartSign(Activity activity) {
        Intent in = new Intent(activity, SignActivity.class);
        activity.startActivity(in);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_41);
    }

    /**
     * 预告
     */
    public static void StartYuGao(Activity activity) {
        StartYuGaoDispose(activity, true);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
    }

    public static void StartYuGao(Activity activity, boolean back) {
        StartYuGaoDispose(activity, back);
    }

    private static void StartYuGaoDispose(Activity activity, boolean back) {
        Intent in = new Intent(activity, YuGaoActivity.class);
        in.putExtra("back_btn", back);
        activity.startActivity(in);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_45);
    }

    /**
     * 认证登录网页
     */
    public static void StartAuthWeb(Activity activity, String url) {
        AppContext.REDIRECT_URI = Url.REDIRECT_URI.Auth_Login;
        Intent in = new Intent(activity, AuthWebActivity.class);
        in.putExtra("url", url);
        activity.startActivity(in);
        activity.overridePendingTransition(R.anim.push_left_in, 0);
        Statistics.onTrackCustomClick(activity, AppConfig.STATISTICS.EVENT_NAME_39);
    }

    /*Uri启动Activity*/
    public static void StartActivityUri(Activity activity, String uri) {
        Intent in = new Intent();
        in.setData(Uri.parse(uri));
        activity.startActivity(in);
        activity.finish();
    }

    /**  查看购物车  **/
    public static void gotoCart(Activity activity){
        //阿里百川购物车
        if (AppContext.isAilError) {
            AppHelper.StartWebView(activity, AppConfig.TAO.CART);
        } else {
            BaiChuan.showCart(activity);
            //BaiChuan.showPage(mActivity, AppConfig.TAO.CART);
        }
        Statistics.onTrackCustomClick(activity,AppConfig.STATISTICS.EVENT_NAME_26);
    }


    public static void startActivityByType(Activity activity, AdvertBean bean, boolean back) {
        startActivityByTypeDispose(activity, bean, back);
    }

    public static void startActivityByType(Activity activity, AdvertBean bean) {
        startActivityByTypeDispose(activity, bean, true);
    }

    /**
     * 根据类型跳至指定页面
     *
     * @Type 1=专题,2=网页,3=品牌,4=分类,5=预告,6=九块频道,7=标签商品
     */
    private static void startActivityByTypeDispose(Activity activity, AdvertBean bean, boolean back) {
        switch (bean.getType()) {
            case 1:
                // 专题
                StartPromotion(activity, bean.getTitle(),
                        bean.getHorizontal_pic_url(), bean.getContent(), back);
                break;
            case 2:
                // 网页
                if (bean.getJump_type() == 1 || AppContext.isAilError) {
                    StartWebView(activity, bean.getContent(), back);
                } else {
                    if (back == false) {
                        StartMain(activity);
                    }
                    BaiChuan.showPage(activity, bean.getTitle(), bean.getContent());
                }
                break;
            case 3:
                // 品牌
                StartBrandItem(activity, bean.getTitle(),
                        bean.getHorizontal_pic_url(), bean.getContent(), back);
                break;
            case 4:
                // 分类
                StartItem(activity, bean.getTitle(), bean.getContent(), Url.toRoute(Url.Item.List, 1), back);
                break;
            case 5:
                // 预告
                StartYuGao(activity, back);
                break;
            case 6:
                // 九块九
                StartItem(activity, bean.getTitle(), bean.getContent(), Url.toRoute(Url.Jiu.List, 1), back);
                break;
            case 7:
                // 标签商品
                StartItem(activity, bean.getTitle(), bean.getContent(), Url.toRoute(Url.Item.Tags, 1), back);
                break;
            default:
                Toast.show(activity, "亲~你的版本过旧了啦~请升级哦~！");
                Uri uri = Uri.parse("http://m.meguo.com/app/index");
                Intent in = new Intent(Intent.ACTION_VIEW, uri);
                activity.startActivity(in);
                break;
        }
    }


}
